package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.domain.BussinessDept;
import com.ruoyi.system.domain.ContractPayDetail;
import com.ruoyi.system.service.IContractPayDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Contract;
import com.ruoyi.system.service.IContractService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 合同Controller
 *
 * @author ruoyi
 * @date 2023-07-18
 */
@RestController
@RequestMapping("/system/contract")
@Slf4j
public class ContractController extends BaseController {
    @Autowired
    private IContractService contractService;
    @Autowired
    private IContractPayDetailService contractPayDetailService;


    @PreAuthorize("@ss.hasPermi('system:contract:list')")
    @GetMapping("/getAll")
    public AjaxResult getAll() {
        List<Contract> list = contractService.list();
        return AjaxResult.success(list);
    }

    @PreAuthorize("@ss.hasPermi('system:contract:list')")
    @GetMapping("/getDetail")
    public AjaxResult getDetail(Long contractId, Long areaId, Long postId, Integer postRank) {
        LambdaQueryWrapper<ContractPayDetail> qw = Wrappers.lambdaQuery();
        qw.eq(ContractPayDetail::getContractId, contractId).eq(ContractPayDetail::getAreaId, areaId)
                .eq(ContractPayDetail::getPostId, postId);
                //.eq(ContractPayDetail::getPostRank, postRank);
        ContractPayDetail one = contractPayDetailService.getOne(qw);
        return AjaxResult.success(one);
    }


    /**
     * 查询合同列表
     */
    @PreAuthorize("@ss.hasPermi('system:contract:list')")
    @GetMapping("/list")
    public TableDataInfo list(Contract contract) {
        startPage();
        List<Contract> list = contractService.selectContractList(contract);
        return getDataTable(list);
    }

    /**
     * 导出合同列表
     */
    @PreAuthorize("@ss.hasPermi('system:contract:export')")
    @Log(title = "合同", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Contract contract) {
        List<Contract> list = contractService.selectContractList(contract);
        ExcelUtil<Contract> util = new ExcelUtil<Contract>(Contract.class);
        util.exportExcel(response, list, "合同数据");
    }

    /**
     * 获取合同详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:contract:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        Contract contract = contractService.getById(id);

        LambdaQueryWrapper<ContractPayDetail> qw = Wrappers.lambdaQuery();
        qw.eq(ContractPayDetail::getContractId, id);
        List<ContractPayDetail> list = contractPayDetailService.list(qw);

        contract.setContractPayDetailList(list);

        log.info("合同：{}", contract);

        return success(contract);
    }

    /**
     * 新增合同
     */
    @PreAuthorize("@ss.hasPermi('system:contract:add')")
    @Log(title = "合同", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Contract contract) {
        //return toAjax(contractService.insertContract(contract));

        return toAjax(contractService.add(contract));

    }

    /**
     * 修改合同
     */
    @PreAuthorize("@ss.hasPermi('system:contract:edit')")
    @Log(title = "合同", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Contract contract) {
        return toAjax(contractService.updateContract2(contract));
    }

    /**
     * 删除合同
     */
    @PreAuthorize("@ss.hasPermi('system:contract:remove')")
    @Log(title = "合同", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {

        int n = contractService.deleteByIds(ids);
        if (n > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error("合同已关联客户，不能删除");
    }
}
